/**
 * 
 */

$(document).ready(function() {
        $("form").submit(function () {
            return false;
        });
        var VAL = {
            NORMAL: "0000000",
            EVT_USER: $("#userId").text(),
            T_FBPBISTOL : "FBPBISTOL",
            T_FIPINQSHT : "FIPINQSHT",
            FSPWIPBNK   : "FSPWIPBNK"

        };
        var domObj= {
                W                 : $(window),
                $prdSeqId         : $("#prdSeqId"),
                wipBankSelect     : $("#wipBankSelect"),
                grid   :{
                        retPrdAListDiv  : $("#retPrdAListDiv"),
                        retPrdAListGrd  : $("#retPrdAListGrd"),
                        retPrdAListPg   : "#retPrdAListPg" ,
                }     
        };
        function initRetPrdAListGrd() {
            domObj.grid.retPrdAListGrd.jqGrid({
                    url: "",
                    datatype: "local",
                    mtype: "POST",
                    height: 400,
                    // width: 1150,
                    autowidth : true,
                    shrinkToFit:false,
                    autoScroll: false,
		    		scroll : true,
		    		resizable : true,
		    		rownumbers : true,
		    		loadonce : true,
		            viewrecords : true,
                    multiselect : true,
                    pager: domObj.grid.retPrdAListPg,
                    colModel: [{
                        name:  'prd_seq_id',
                        index: 'prd_seq_id',
                        label: '屏体代码',
                        width: 200
                    }, {
                        name: 'prd_stat',
                        index: 'prd_stat',
                        label: '屏体状态',
                        width: 180
                    }, {
                        name:  'tool_id_fk',
                        index: 'tool_id_fk',
                        label: '在制仓名',
                        width: 200
                    }, {
                        name:  'wo_id_fk',
                        index: 'wo_id_fk',
                        label: '工单号',
                        width: 210
                    }, {
                        name:  'mdl_id_fk',
                        index: 'mdl_id_fk',
                        label: '产品代码',
                        width: 210
                    },{
                        name:  'path_id_fk',
                        index: 'path_id_fk',
                        label: '工艺路线',
                        width: 210
                    }, {
                        name:  'ope_id_fk',
                        index: 'ope_id_fk',
                        label: '站点信息',
                        width: 180
                    }, {
                        name:  'prd_grade',
                        index: 'prd_grade',
                        label: '屏体等级',
                        width: 150
                    }]
                })
        };
        initRetPrdAListGrd();
        var toolFunc = {
            iniWipBankSelect: function () {
                var inObj, outObj;
                inObj = {
                    trx_id: VAL.T_FBPBISTOL,
                    action_flg: 'Q',
                    iary: [
                        {tool_cate: 'WPBK'}
                    ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    SelectDom.initWithSpace($("#wipBankSelect"));
                    SelectDom.addSelectArr($("#wipBankSelect"), outObj.oary, "tool_id","tool_id","",true);
                }
            },
        };
        var btnFunc = {
            query_func: function () {
                var inObj, outObj,  prdSeqId;
                prdSeqId = $.trim(domObj.$prdSeqId.val());
                if (!prdSeqId) {
                    showErrorDialog("", "屏体代码不能为空");
                    return false;
                }
                var rowDatas = domObj.grid.retPrdAListGrd.jqGrid("getRowData");
				if(rowDatas.length>0){
					for (var i = 0; i < rowDatas.length; i++) {
						if ($.trim(rowDatas[i].prd_seq_id) == prdSeqId ) {
                            showErrorDialog("", "["+prdSeqId+"]屏体信息已经存在列表中");
                            domObj.$prdSeqId.val("");
                            return false;
						}
					}
				}
                inObj={
                    trx_id : VAL.T_FIPINQSHT,
                    action_flg : "Q",
                    prd_seq_id : prdSeqId
                }
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    if(outObj.prd_stat=="WAIT"){
                        if(rowDatas.length>0){
                            for (var i = 0; i < rowDatas.length; i++) {
                                if(outObj.wo_id_fk!=rowDatas[i].wo_id_fk){
                                    showErrorDialog("", "工单信息不一致");
                                    return false;
                                }
                                if(outObj.mdl_id_fk!=rowDatas[i].mdl_id_fk){
                                    showErrorDialog("", "产品信息不一致");
                                    return false;
                                }
                                if(outObj.path_id_fk!=rowDatas[i].path_id_fk){
                                    showErrorDialog("", "工艺路线不一致");
                                    return false;
                                }
                                if(outObj.ope_id_fk!=rowDatas[i].ope_id_fk){
                                    showErrorDialog("", "站点信息不一致");
                                    return false;
                                }
                                if(outObj.tool_id_fk!=rowDatas[i].tool_id_fk){
                                    showErrorDialog("", "在制仓信息不一致");
                                    return false;
                                }
                                var data = outObj
                                var newRowId = getGridNewRowID("#retPrdAListGrd");
                                domObj.grid.retPrdAListGrd.jqGrid("addRowData", newRowId, data);
                            }
                        }else {//第一条数据直接添加
                            var data = outObj
                            var newRowId = getGridNewRowID("#retPrdAListGrd");
                            domObj.grid.retPrdAListGrd.jqGrid("addRowData", newRowId, data);
                        }
                    }else {
                        showErrorDialog("", "ID["+prdSeqId+"]屏体状态不是WAIT");
                        return false;
                    }
                }
            },
            //返回产线
            bank_out_func : function(){
                 var i, inObj,outObj,rowIds,iary = [];
                    rowIds = domObj.grid.retPrdAListGrd.jqGrid('getGridParam','selarrrow');
                     if(rowIds.length === 0 ){
                         showErrorDialog("","请先勾选需要返回产线的屏体！");
                         return false;
	                 }
	                 var idLen = rowIds.length;
	                 for( i=0; i < idLen; i++){
	                     var  rowData = domObj.grid.retPrdAListGrd.jqGrid('getRowData', rowIds[i]);
	                      iary.push({
                              prd_seq_id : rowData.prd_seq_id
	                      });
	                 }
	                 inObj = {
                         trx_id     : VAL.FSPWIPBNK ,
                         action_flg : 'O',
                         evt_usr    : VAL.EVT_USER,
                         iary       : iary
                     };
	                 outObj = comTrxSubSendPostJson(inObj);
                     if (outObj.rtn_code == VAL.NORMAL) {
                         for(var i=0;i<idLen;i++){
                             domObj.grid.retPrdAListGrd.jqGrid("delRowData",rowIds[0]);
                         }
                         showSuccessDialog("返回产线成功");
                     }
	                 return true;
            },
            //转入在制仓
               bank_in_func : function(){
                   var inObj, outObj, tool_id;
                   var rowIds = $("#retPrdAListGrd").jqGrid("getGridParam",'selarrrow');
                   if(rowIds.length == 0){
                	   showErrorDialog('',"请选择需要转入在制仓的屏体");
                       return false;
                   }
                   var iary = new Array();
                   for(var i=0;i<rowIds.length;i++){
                	   var rowData = $("#retPrdAListGrd" ).jqGrid("getRowData",rowIds[i]);
                	   var iaryA={
                		   prd_seq_id : rowData.prd_seq_id
                	   }
                	   iary.push(iaryA);
                   }
                   tool_id = $.trim(domObj.wipBankSelect.val());
                       if(!tool_id){
                       showErrorDialog('','请选择在制仓！');
                       return false;
	                }
	                inObj = {
                         trx_id : VAL.FSPWIPBNK,
                         action_flg : 'I',
                         evt_usr    : VAL.EVT_USER,
                         iary       : iary,
                         tool_id    : tool_id
                    };
                   outObj = comTrxSubSendPostJson(inObj);
                   if (outObj.rtn_code == VAL.NORMAL) {
                       var idLen = rowIds.length;
                       for(var i=0;i<idLen;i++){
                           domObj.grid.retPrdAListGrd.jqGrid("delRowData",rowIds[0]);
                       }
                       showSuccessDialog("转入在制仓成功");
                   }
            }

        };

    domObj.$prdSeqId.keydown(function(event) {
        if (event.which == 13) {
            btnFunc.query_func();
        }
    });


    var iniButtonAction = function() {
        $("#query_btn").click(function () {
            btnFunc.query_func();
        });
        $("#query_wipbank_btn").click(function () {
            btnFunc.query_wipbank_func();
        });
        $("#into_btn").click(function () {
            btnFunc.bank_in_func();
        });
        $("#return_btn").click(function () {
            btnFunc.bank_out_func();
        });
        $("#refresh_btn").click(function () {
        	iniContorlData();
            domObj.$prdSeqId.val("");
        	$("#retPrdAListGrd").clearGridData();
        });
    };

    var iniContorlData = function(){
        toolFunc.iniWipBankSelect();
        $("form").submit(function(){
            return false;
        });
    };
    var initializationFunc = function(){
        domObj.$prdSeqId.val("");
        iniButtonAction();
        iniContorlData();
    };
    initializationFunc();
    //enter
   /* document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
    //表格自适应
    function resizeFnc(){
        var offsetBottom, grdDivWidth;
        grdDivWidth = $("#divTotal").width();
        offsetBottom = $(window).height() - $("#retPrdAListDiv").offset().top;

        $("#retPrdAListDiv").width(grdDivWidth*0.98);
        $("#retPrdAListDiv").height(offsetBottom*0.99);
        $("#retPrdAListGrd").setGridWidth(grdDivWidth*0.97);
        $("#retPrdAListGrd").setGridHeight(offsetBottom*0.95-51);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});